//
//  ScanViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/24.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "ScanViewController.h"
#import "ScanModel.h"

#import "UUChart.h"

@interface ScanViewController () <UUChartDataSource>

@end

@implementation ScanViewController
{
    NSMutableArray *_dataArray;
    NSMutableArray *_xArr;
    NSMutableArray *_yArr;
    UserInfoManager *_userInfo;
    ScanModel *_scanModel;
    UUChart *_chartView;
    
    UILabel *_today;
    UILabel *_yesterday;
    UILabel *_week;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"教师主页浏览量"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];

    
    _userInfo = [UserInfoManager sharedUserInfoManager];
    [_userInfo loadUserInfoFromSanbox];
    
    [self creatFirstHttpRequest];
    [self creatChartView];
    [self creatBaseView];
}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatFirstHttpRequest
{
    NSDictionary *param = @{
                            @"teacher_Id":_userInfo.memberModel.member_Id,
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(WEAKSCAK) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            
            if (_xArr) {
                [_xArr removeAllObjects];
            }else{
                _xArr = [[NSMutableArray alloc] init];
            }
            
            
            if (_yArr) {
                [_yArr removeAllObjects];
            }else{
                _yArr = [[NSMutableArray alloc] init];
            }
            
            
            [ScanModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"ScanData",
                         };
            }];
            
            _scanModel = [[ScanModel alloc] init];
            _scanModel = [ScanModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_scanModel.data];
            
            for (ScanData *data in _dataArray) {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSString *date = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[data.date_time integerValue]/1000]];
                [_xArr addObject:date];
                
                if (data.total) {
                    [_yArr addObject:data.total];
                }else{
                    [_yArr addObject:@"0"];
                }
            }
            
            if (_dataArray.count > 0) {
                [_chartView showInView:self.view];
            }
            
            [self refreshDateWithXArr:_xArr YArr:_yArr];
            
        }
    }];
}


- (void)refreshDateWithXArr:(NSArray *)x YArr:(NSArray *)y
{
    NSInteger i = 0;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *todayDate = [dateFormatter stringFromDate:[NSDate date]];
    NSString *yestodayDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:-24*60*60 sinceDate:[NSDate date]]];
    
    for (NSString *date in x) {
        if ([date isEqualToString:todayDate]) {
            _today.text = [NSString stringWithFormat:@"今日浏览量：%@",y[i]];
        }
        
        if ([date isEqualToString:yestodayDate]) {
            _yesterday.text = [NSString stringWithFormat:@"昨日浏览量：%@",y[i]];
        }
        
        i ++;
    }
}



- (void)creatChartView
{
    _chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(10, 74, ECSCREEN_W - 20, 150)
                                               withSource:self
                                                withStyle:UUChartLineStyle];
    _chartView.backgroundColor = BG_COLOR;
}


- (void)creatBaseView
{
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 74 + 150, ECSCREEN_W, ECSCREEN_H - 74 - 150)];
    [self.view addSubview:baseView];
    
    
    _today = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, ECSCREEN_W - 20, 20)];
    _today.text = @"今日浏览量：0";
    _today.textColor = ECCOLOR(0, 0, 0, 1);
    _today.font = [UIFont systemFontOfSize:13.f];
    [baseView addSubview:_today];
    
    _yesterday = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, ECSCREEN_W - 20, 20)];
    _yesterday.text = @"昨日浏览量：0";
    _yesterday.textColor = ECCOLOR(0, 0, 0, 1);
    _yesterday.font = [UIFont systemFontOfSize:13.f];
    [baseView addSubview:_yesterday];

    
    NSInteger scanNum = 0;
    for (ScanData *data in self.model.data) {
        scanNum += [data.total integerValue];
    }
    _week = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, ECSCREEN_W - 20, 20)];
    _week.text = [NSString stringWithFormat:@"本周浏览量：%ld",scanNum];
    _week.textColor = ECCOLOR(0, 0, 0, 1);
    _week.font = [UIFont systemFontOfSize:13.f];
    [baseView addSubview:_week];
}



#pragma mark - UUChart_X
- (NSArray *)UUChart_xLableArray:(UUChart *)chart
{
    
    if (_xArr.count == 0) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *todayDate = [dateFormatter stringFromDate:[NSDate date]];
        [_xArr addObject:todayDate];
    }
    return _xArr;
}


#pragma mark - UUChart_Y
//数值多重数组
- (NSArray *)UUChart_yValueArray:(UUChart *)chart
{
    if (_yArr.count == 0) {
        [_yArr addObject:@"0"];
    }
    
    return @[_yArr];
}


#pragma mark - @optional
//颜色数组
- (NSArray *)UUChart_ColorArray:(UUChart *)chart
{
    return @[UURed];
}


- (BOOL)UUChart:(UUChart *)chart ShowHorizonLineAtIndex:(NSInteger)index
{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
